package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CategoryInfoMapper;
import com.ruoyi.system.domain.CategoryInfo;
import com.ruoyi.system.service.ICategoryInfoService;

/**
 * 产品层级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class CategoryInfoServiceImpl implements ICategoryInfoService
{
    @Autowired
    private CategoryInfoMapper categoryInfoMapper;

    /**
     * 查询产品层级
     *
     * @param categoryId 产品层级主键
     * @return 产品层级
     */
    @Override
    public CategoryInfo selectCategoryInfoByCategoryId(String categoryId)
    {
        return categoryInfoMapper.selectCategoryInfoByCategoryId(categoryId);
    }

    /**
     * 查询产品层级列表
     *
     * @param categoryInfo 产品层级
     * @return 产品层级
     */
    @Override
    public List<CategoryInfo> selectCategoryInfoList(CategoryInfo categoryInfo)
    {
        return categoryInfoMapper.selectCategoryInfoList(categoryInfo);
    }

    @Override
    public List<CategoryInfo> selectCategoryInfoListTree() {
        List<CategoryInfo> list = categoryInfoMapper.selectCategoryInfoList(new CategoryInfo());
        return listToTree(list);
    }

    private static List<CategoryInfo> listToTree(List<CategoryInfo> list) {
        // 要点
        Map<String, List<CategoryInfo>> parentMap = list.stream().collect(Collectors.groupingBy(CategoryInfo::getParentId));
        list.forEach(item -> {
            item.setChildren(parentMap.get(item.getCategoryId()));
        });
        // 过滤出根节点集合,根节点已经包含了孩子节点
        return list.stream().filter(item -> "0".equals(item.getParentId())).collect(Collectors.toList());
    }

    /**
     * 新增产品层级
     *
     * @param categoryInfo 产品层级
     * @return 结果
     */
    @Override
    public int insertCategoryInfo(CategoryInfo categoryInfo)
    {
        if (StringUtils.isEmpty(categoryInfo.getCategoryId()) || StringUtils.isEmpty(categoryInfo.getCategoryName()) || StringUtils.isEmpty(categoryInfo.getParentId())){
            throw new SecurityException("信息填写错误，请重新填写");
        }else {
            CategoryInfo category = categoryInfoMapper.selectCategoryInfoByCategoryId(categoryInfo.getCategoryId());
            if (ObjectUtils.isNotEmpty(category)){
                throw new SecurityException("此编码已存在，请重新输入类别编码");
            }
        }
        categoryInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return categoryInfoMapper.insertCategoryInfo(categoryInfo);
    }

    /**
     * 修改产品层级
     *
     * @param categoryInfo 产品层级
     * @return 结果
     */
    @Override
    public int updateCategoryInfo(CategoryInfo categoryInfo)
    {
        categoryInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return categoryInfoMapper.updateCategoryInfo(categoryInfo);
    }

    /**
     * 批量删除产品层级
     *
     * @param categoryIds 需要删除的产品层级主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInfoByCategoryIds(String[] categoryIds)
    {
        return categoryInfoMapper.deleteCategoryInfoByCategoryIds(categoryIds);
    }

    /**
     * 删除产品层级信息
     *
     * @param categoryId 产品层级主键
     * @return 结果
     */
    @Override
    public int deleteCategoryInfoByCategoryId(String categoryId)
    {
        Long count = categoryInfoMapper.selectCountByParentId(categoryId);
        if (count > 0){
            throw new SecurityException("该分类存在下级分类，无法删除");
        }
        return categoryInfoMapper.deleteCategoryInfoByCategoryId(categoryId);
    }
}
